package com.handpay.zztong.hp.db.ormlite;

import android.content.Context;

import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.component.ComponentItemBean;
import com.handpay.zztong.hp.main.model.component.ComponentMappingItemBean;
import com.handpay.zztong.hp.main.model.component.ComponentRelationItemBean;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyang on 2017/4/11.
 */

public class DataBaseComponentModel {
    private DataBaseHelper mDataBaseHelper;
    private Dao<ComponentItemBean, String> mComponentDao;
    private Dao<ComponentRelationItemBean, Integer> mComponentRelationDao;
    private Dao<ComponentMappingItemBean, Integer> mComponentMappingDao;


    public DataBaseComponentModel(Context context) {
        mDataBaseHelper = DataBaseHelper.getDBHelper(context);
        try {
            mComponentDao = getComponentDao();
            mComponentRelationDao = getComponentRelationDao();
            mComponentMappingDao = getComponentMappingDao();

        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "Get noticeDao faile" + e);
        }
    }

    /***
     * @return 得到业务操作类
     * @throws SQLException sql异常
     */
    private Dao<ComponentItemBean, String> getComponentDao() throws SQLException {
        if (mComponentDao == null) {
            mComponentDao = mDataBaseHelper.getDao(ComponentItemBean.class);
        }
        return mComponentDao;
    }

    /***
     * @return 得到业务操作类
     * @throws SQLException sql异常
     */
    private Dao<ComponentMappingItemBean, Integer> getComponentMappingDao() throws SQLException {
        if (mComponentMappingDao == null) {
            mComponentMappingDao = mDataBaseHelper.getDao(ComponentMappingItemBean.class);
        }
        return mComponentMappingDao;
    }

    /***
     * @return 得到业务操作类
     * @throws SQLException sql异常
     */
    private Dao<ComponentRelationItemBean, Integer> getComponentRelationDao() throws SQLException {
        if (mComponentRelationDao == null) {
            mComponentRelationDao = mDataBaseHelper.getDao(ComponentRelationItemBean.class);
        }
        return mComponentRelationDao;
    }


    public void addComponent(ComponentItemBean component) {
        if (isExistComponent(component)) {
            deleteComponent(component);
        }
        try {
            mComponentDao.create(component);//如果消息不存在则添加
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ComponentItemBean queryComponent(ComponentItemBean component) {
        ComponentItemBean componentItemBean = null;
        try {
            componentItemBean = mComponentDao.queryBuilder().where().eq("itemCode", component.getItemCode()).queryForFirst();
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "query Componentfailed！" + e);
        }
        return componentItemBean;
    }

    public ComponentItemBean queryComponent(String itemCode) {
        ComponentItemBean componentItemBean = null;
        try {
            componentItemBean = mComponentDao.queryBuilder().where().eq("itemCode", itemCode).queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return componentItemBean;
    }


    /**
     * 查询所有的通知消息
     *
     * @return 返回数据库通知消息(实现分页查询)
     */
    public List<ComponentItemBean> queryComponentList() {
        List<ComponentItemBean> list = null;
        try {
//
            list = mComponentDao.queryBuilder().orderBy("updateTime",false).query();
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "queryComponentList  faile！" + e);
        }
        return list;
    }


    public boolean isExistComponent(ComponentItemBean component) {
        if (queryComponent(component) != null) {
            return true;
        }
        return false;
    }

    public void deleteComponent(ComponentItemBean component) {
        try {
            mComponentDao.delete(queryComponent(component));
            HPLog.d(HPLog.YL_TAG, "delete Component");
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "delete Component failed！" + e);
        }
    }


    public void deleteAll() {
        try {
            mComponentDao.delete(mComponentDao.queryForAll());
            mComponentRelationDao.delete(mComponentRelationDao.queryForAll());
            mComponentMappingDao.delete(mComponentMappingDao.queryForAll());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void addComponentMapping(ComponentMappingItemBean componentMapping) {
        if (isExistComponentMapping(componentMapping)) {
            deleteComponentMapping(componentMapping);
        }
        try {
            mComponentMappingDao.create(componentMapping);//如果消息不存在则添加
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public boolean isExistComponentMapping(ComponentMappingItemBean component) {
        if (queryComponentMapping(component) != null) {
            return true;
        }
        return false;
    }

    public List<ComponentMappingItemBean> queryComponentMappingList() {
        List<ComponentMappingItemBean> list = null;
        try {
            list = mComponentMappingDao.queryBuilder().query();
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "queryComponentMappingList faile！" + e);
        }
        return list;
    }

    public ComponentMappingItemBean queryComponentMapping(ComponentMappingItemBean componentMapping) {
        ComponentMappingItemBean bean = null;
        try {
            bean = mComponentMappingDao.queryBuilder().where().eq("itemCode", componentMapping.getItemCode()).and().eq("showArea", componentMapping.getShowArea()).queryForFirst();
//            bean = mComponentMappingDao.queryBuilder().where().eq("itemCode", componentMapping.getItemCode()).queryForFirst();
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "queryComponentMapping faile！" + e);
        }
        return bean;
    }

    public List<ComponentMappingItemBean> queryComponentMappingByShowArea(String showArea) {
        List<ComponentMappingItemBean> list = null;
        try {
            list = mComponentMappingDao.queryBuilder().orderBy("orderNum", true).where().eq("showArea", showArea).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void deleteComponentMappingByShowArea(String showArea) {
        try {
            mComponentMappingDao.delete(queryComponentMappingByShowArea(showArea));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteComponentMapping(ComponentMappingItemBean componentMapping) {
        try {
            mComponentMappingDao.delete(queryComponentMapping(componentMapping));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<ComponentRelationItemBean> queryComponentRelationList() {
        List<ComponentRelationItemBean> list = null;
        try {
            list = mComponentRelationDao.queryBuilder().query();
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "queryComponentRelationList faile！" + e);
        }
        return list;
    }

    public ComponentRelationItemBean queryComponentRelation(ComponentRelationItemBean componentRelation) {
        ComponentRelationItemBean result = null;
        try {
            result = mComponentRelationDao.queryBuilder().where().eq("itemCode", componentRelation.getItemCode()).queryForFirst();
        } catch (SQLException e) {

        }
        return result;
    }

    public List<ComponentRelationItemBean> queryComponentRelationByParentItemCode(String parentItemCode) {
        List<ComponentRelationItemBean> list = null;
        try {
            list = mComponentRelationDao.queryBuilder().where().eq("parentItemCode", parentItemCode).query();
        } catch (SQLException e) {

        }
        return list;
    }

    public void addComponentRelation(ComponentRelationItemBean componentRelation) {
        if (isExistComponentRelation(componentRelation)) {
            deleteComponentRelation(componentRelation);
        }
        try {
            mComponentRelationDao.create(componentRelation);//如果消息不存在则添加
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public boolean isExistComponentRelation(ComponentRelationItemBean component) {
        if (queryComponentRelation(component) != null) {
            return true;
        }
        return false;
    }

    public void deleteComponentRelation(ComponentRelationItemBean component) {
        try {
            mComponentRelationDao.delete(queryComponentRelation(component));
            HPLog.d(HPLog.YL_TAG, "deleteComponentRelation");
        } catch (SQLException e) {
            HPLog.e(HPLog.YL_TAG, "deleteComponentRelation  failed！" + e.toString());
        }
    }

    public void deleteComponentRelationByParentId(String parentId) {
        try {
            mComponentRelationDao.delete(queryComponentRelationByParentItemCode(parentId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}